
var target_dialog=function(the_target_id,the_id,type,operate,img,user_id,user_name,index){
	$('#'+the_id).empty();
	$('#'+the_id).append('<div class="dialog"></div><div class="my_dialog"></div><div class="my_dialog" ></div>');
	$('#'+the_id+' div.my_dialog:eq(0)').append('<h4>目标详情</h4><i class="fa fa-times"></i><div class="target_dialog" style="padding:0.5em 1em;background:white"></div>');
	if(operate=='0'&& type=='6')
		$('#'+the_id+' div.my_dialog:eq(0)').append('<ul class="target_operate"><li >更多操作<i class="fa fa-chevron-down"></i><ul> <li>分享该目标</li><li>删除该目标</li></ul></li></ul>');
	else if(type!='6'&&operate=='1')
		$('#'+the_id+' div.my_dialog:eq(0)').append('<ul class="target_operate"><li >更多操作<i class="fa fa-chevron-down"></i><ul><li>分享到广场</li><li >取消加入</li></ul></li></ul>');
    else if(operate=='1'&& type=='6')
	  $('#'+the_id+' div.my_dialog:eq(0)').append('<ul class="target_operate"><li >更多操作<i class="fa fa-chevron-down"></i><ul><li>分享到广场</li><li >取消加入</li> <li>删除该目标</li></ul></li></ul>');
    else
    	$('#'+the_id+' div.my_dialog:eq(0)').append('<ul class="target_operate"><li >更多操作<i class="fa fa-chevron-down"></i><ul><li>分享到广场</li></ul></li></ul>');
	$('#'+the_id+'  div.my_dialog:eq(0)').append('<button class="make_sure" style="margin-right:2.5em"> 确定</button>');
	$('#'+the_id+' div.my_dialog:eq(1)').append( '<h4>目标分享</h4><i class="fa fa-times"></i><div style="padding:0.5em 1em;background:white">'+
             '<textarea    name="target_describe"></textarea>'+
             '<button class="target_share">分享到广场</button><img class="the_user" src="'+img+'"/><h4 >智行校园</h4><span >我的目标分享</span>'+
            ' <textarea  disabled="disabled" name="target_describe"></textarea>'+
            '<div class="share_dialog" style="background:#eeeeee;margin: 0 1.5em;padding-bottom:1em"><h3></h3><p style="height:1em"></p>'+
            '<span id="toggle_article" >展开全文</span><h3 class="table-toggle">展开分解任务<i class="fa fa-caret-right fa-lg" ></i> </h3><div>'+
            '<table id="dialog_table_share"></table></div></div></div>');
	var detail_colums = [[ "id","c_name", "c_resource","total","can_sign" ],
			              [ "序号", "描述", "相关资料", "签到次数" ,"今日签到"] ];
    var detail_share = [[ "id","c_name","c_resource", "total" ],
		              [ "序号", "描述", "相关资料", "签到次数" ] ];
    $('#'+the_id+' div.my_dialog:eq(1) textarea:eq(0)').val('#晒我的目标#每天完成一个小目标，虽然微小，但千里之行，始于足下.终有一天我会实现我的大目标#智行校园之我的目标# https://mb.yunhua.com/ebill/index.html');
    $('#'+the_id+' div.my_dialog:eq(1) textarea:eq(1)').val('#晒我的目标#每天完成一个小目标，虽然微小，但千里之行，始于足下.终有一天我会实现我的大目标#智行校园之我的目标# https://mb.yunhua.com/ebill/index.html');
    var the_url=operate=='1'? '/WDDXSERVER/target/myJoinedTarget':'/WDDXSERVER/target/myNotJoinedTarget'
	 $.ajax({
		 url:the_url,
		 data:{targetID:the_target_id,userid:user_id,ticket:ticket},
		 type:'post'
	 }).done(function(data){
		 data = data.content.message;
		 var img='';
		 $('#'+the_id+'  div.my_dialog:eq(0) div.target_dialog').append('<h3>'+data.t_name+'</h3><button><i class="fa fa-share-alt"></i>分享到广场</button>');
		 $('#'+the_id+'  div.my_dialog:eq(0) div.target_dialog').append('<p style="height:1em"><font size="3" >描述：</font>'+data.t_describe+'</p><span id="toggle_article" >展开全文</span>');
		 if(data.sign_count!='0'){
		  for(var i=0;i<data.sign_img.length;i++)
			  img+='<img src="'+data.sign_img[i]+'"/>';
		  if(data.sign_img.length<9)
			  $('#'+the_id+'  div.my_dialog:eq(0) div.target_dialog').append('<p style="margin:0"><font size="3" >今日签到：</font>'+img+'</p>');
		  else
		      $('#'+the_id+'  div.my_dialog:eq(0) div.target_dialog').append('<p style="margin:0"><font size="3" >今日签到：</font>'+img+'...共'+data.sign_count+'人</p>');
		 }
		 $('#'+the_id+'  div.my_dialog:eq(0) div.target_dialog').append('<font size="3" style="width:100%;margin-left:1.5em;display:block">目标分解：</font><div style="margin:0.2em 1.5em;width:calc(100% - 3em);"><table id="dialog_table"></table></div>');
		 $('div#'+the_id).show('slow');
		 var the_colum= operate=='1'? detail_colums : detail_share;
					table("dialog_table",data.details,the_colum, false,150,operate);
					$('div.my_dialog span#toggle_article').click(function(){
						$(this).text()=='展开全文'? $(this).text('收起').prev().css('height','auto')
								:$(this).text('展开全文').prev().css('height','1em');
					});
					$('button.make_sure').unbind('click').click(function(){
					
						if(operate=='1'){
						var target = new Array();
	    		        var row = new Object();
	    		        var sign_info=new Array();
	    		        row.target_id = the_target_id;
	    	            row.sign = sign_info;

	    		        for(var i=0;i<$('table#dialog_table').bootstrapTable('getOptions').data.length;i++){
	    		        	if($('table#dialog_table').bootstrapTable('getOptions').data[i].c_completion=='0'){
	    		               var sign_row = new Object();
	    		               sign_row.c_id =$('#dialog_table').bootstrapTable('getOptions').data[i].c_id;
	    		              sign_row.sign = $('#'+the_id+' tbody  tr:eq('+i+') td:eq(4) img').attr('data-status');
	    		              sign_info.push(sign_row);}
	    		         }
	    		        target.push(row);
	    		        $.ajax({
		    		    	   url:'/WDDXSERVER/target/target_QD',
		    		    	   data:{userid:user_id,items:JSON.stringify(target),ticket:ticket},
		    		    	   type:'post'
		    		       });}
						$('div#'+the_id).empty();
						$('div#'+the_id).hide('fast');
					});
					
					$('#'+the_id+' div.my_dialog:eq(0) button:contains(分享到广场),ul.target_operate ul li:contains(分享到广场)').click(function(){
						  $('#'+the_id+' div.my_dialog:eq(1) div.share_dialog h3:eq(0)').text(data.t_name);
						  $('#'+the_id+' div.my_dialog:eq(1) div.share_dialog p').html('<font size="3" >描述：</font>'+data.t_describe);
						  table("dialog_table_share",data.details,detail_share, false,250,operate);
						  $('#'+the_id+' div.my_dialog:eq(1)').toggle();
						  $('#'+the_id+' div.my_dialog:eq(1) i.fa-caret-right').unbind('click').click(function(){
							  $('#'+the_id+' div.my_dialog:eq(1) h3.table-toggle').next().toggle();
						  });
						  $('#'+the_id+' div.my_dialog:eq(1) i.fa-times').unbind('click').click(function(e){
							  $('div#'+the_id+' div.my_dialog:eq(1)').hide('fast');
							  e.stopPropagation();
						  });
						  $('#'+the_id+' div.my_dialog:eq(1) button.target_share').unbind('click').click(function(e){
							  $.ajax({
			    		    	   url:'/WDDXSERVER/target/shareTarget',
			    		    	   data:{targetID:the_target_id,ticket:ticket,userid:user_id,username:encodeURI(encodeURI(user_name)),Devicename:' ',content:encodeURI(encodeURI($(this).prev().val()))},
			    		    	   type:'post',
			    		    	   success:function(){
			    		    		   $('#'+the_id+' div.my_dialog:eq(1)').hide('fast');
			    		    		   win.alert("系统提示","分享成功！");
			    		    	   }
			    		       });
							  e.stopPropagation();
						  });
						  $('#'+the_id+' div.my_dialog:eq(1) textarea:eq(0)').bind('focus',function(){
							  $(this).val('');
							  $('#'+the_id+' div.my_dialog:eq(1) textarea:eq(0)').bind('input propertychange', function() {
								  $('#'+the_id+' div.my_dialog:eq(1) textarea:eq(1)').val($(this).val());
							  });
						  })
						 
					});
					$('div.my_dialog:eq(0) i.fa-times').click(function(){
						$('div#'+the_id).hide('fast');
						$('div#'+the_id).empty();
					});
					$('ul.target_operate').unbind('hover').hover(function(){
					    $(this).find('ul').show('fast');
					},function(){
						$(this).find('ul').hide('fast');
					});
					
					 $('ul.target_operate ul li:contains(取消加入)').unbind('click').click(function(event){
						 $('div#'+the_id).hide('fast');
							$('div#'+the_id).empty();
				    	 $.ajax({
		    		    	   url:'/WDDXSERVER/target/RemovedFromTheUserTargetBase',
		    		    	   data:{userid:user_id,targetID:the_target_id,ticket:ticket},
		    		    	   type:'post',
		    		    	   success:function(){
		    		    		   $('#the_table').bootstrapTable('updateCell',{index:index, field:'target_operate', value:'0'});
		    		    		   win.alert("系统提示","已从您的目标中移出该目标！");
		    		    	   }
		    		       });
				    	 event.stopPropagation()
				    	 
				    });
					 $('ul.target_operate ul li:contains(删除该目标)').click(function(event){
						 $('div#'+the_id).hide('fast');
							$('div#'+the_id).empty();
						 win.confirm('系统提示', '是否确定删除此目标？');
						 $('div.body-panel p.btns button:eq("0")').unbind('click').click(function(){
			    		        win.close();
			    		       $.ajax({
			    		    	   url:'/WDDXSERVER/target/deleteTarget',
			    		    	   data:{targetID:the_target_id,userid:user_id,ticket:ticket},
			    		    	   type:'post',
			    		    	   success:function(){
			    		    		   $('#the_table').bootstrapTable('remove',{ field:'target_id', values:[the_target_id]});
			    		    		   win.alert("系统提示","目标已删除成功");
			    		    	   }
			    		       });
			    		 });
				    	 event.stopPropagation();
				    	 
				    });
	 });

    dialog_move($('#'+the_id+' div.my_dialog:eq(0)'));
    dialog_move($('#'+the_id+' div.my_dialog:eq(1)'));
};
function table(id, data,colums, pagination,height,operate) {
	var colum = [];
	for (var i = 0; i < colums[0].length; i++){
		    if(i==0)
		       colum[i]={field:colums[0][i],title:colums[1][i],valign:"middle",align:"center",formatter:actionFormatter_text};
		    else if( i==(colums[0].length-2)&&id=='dialog_table'&&operate=='1')
			       colum[i]={field:colums[0][i],title:colums[1][i],valign:"middle",align:"center",formatter:actionFormatter_sign};
		    else if( i==(colums[0].length-1)&&id=='dialog_table'&&operate=='1')
			       colum[i]={field:colums[0][i],title:colums[1][i],valign:"middle",align:"center",formatter:actionFormatter_img,events: operateEvents_img};
		    else
    		   colum[i]={field:colums[0][i],title:colums[1][i],valign:"middle",align:"center"};
	}
	$('#' + id).bootstrapTable({
		columns : colum,
		height:height,
		data : data,
		rowStyle:  function (row, index) {
           if(row.c_completion=='1'||row.can_sign=='0')
            	return {classes: 'active target_completed'};
           else
        	  return {};	
          },
		pagination : pagination,
		search : false,
		searchOnEnterKey : true,
		locale : 'zh-CN'
	});		
};
function dialog_move(object){
	object.hover(function(){
		$(this).css('cursor','move');
	},function(){
		$(this).css('cursor','default');
	});
	object.mousedown(
	            function(event){
	    	//	$(this).css('cursor','move');
	                var isMove = true;
	                var screenW =$(document).width(); 
	                var screenH = $(document).height();
	                var dialogW=object.outerWidth(true);
	                var dialogH=object.outerHeight(true);
	                var abs_x = event.pageX - object.offset().left; 
	                var abs_y = event.pageY - object.offset().top;
	    			
	                $(document).mousemove(function (event) {  
	                        if (isMove) {  
	                            var obj = object; 
	                            var left_x = event.pageX - abs_x;
	                            var top_y = event.pageY - abs_y;
	    					//	console.log(top_y);
	                            if((screenW-dialogW)<=left_x){
	                                left_x = screenW-dialogW;
	                            }
	                            if((screenH-dialogH)<=top_y){
	                                top_y = screenH-dialogH;
	                            }
	                            if(left_x<0){
	                                left_x=20;
	                            }
	                            if(top_y<0){
	                                top_y=10;
	                            }
	    						
	                            obj.css({'left':left_x, 'top':top_y});  
	                        }  
	                    }
	                 ).mouseup(  
	                                function () {  
	                                	
	                                       isMove = false;  
	                                    }  
	                                );
	            }
	        );
};
function actionFormatter_sign(value, row, index) {
	   return [row.c_sign_times+'/'+row.total].join('');
	};
function actionFormatter_text(value, row, index) {
   return ['第'+(index+1)+'项'].join('');
};
function actionFormatter_a(value, row, index) {
    return [
    '<a target="_blank" href="'+value+'">资料</a>'
     ].join('');
};
function actionFormatter_img(value, row, index) {
	 if(value=='1'||row.completed=='1')
		return [ '<img   alt="'+value+'" data-status="0" src="../../img/sign_ok.gif" />'].join('');
     else
       return [ '<img  alt="'+value+'" data-status="1" src="../../img/sign_no.png" />'].join('');
	 };
window.operateEvents_img = {
	    		'click img': function (e, value, row, index) {
	    			if(row.c_completion=='0'&&row.can_sign=='1')
	    			 $(this).attr('alt')=='1'?(
	    					    $(this).attr('src','../../img/sign_no.png'),
	    					    $(this).attr('data-status','1'),
	    					    $(this).attr('alt','0'),
	    					    $(this).parent().prev().text(parseInt($(this).parent().prev().text().split('/')[0])+1+'/'+row.total)
	    					   ):
	    				       ( 
	    				        $(this).attr('src','../../img/sign_ok.gif'),
	    				        $(this).attr('data-status','0'),
	    				        $(this).attr('alt','1'),
	    					    $(this).parent().prev().text(parseInt($(this).parent().prev().text().split('/')[0])-1+'/'+row.total)
	    					   );
	    		  }
	    	  };